﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Lead2012.Business;
using Lead2012.Models;
using Lead2012.ViewModels;
using Lead2012.Repository;
using Lead2012.Utility;

namespace Lead2012.Controllers
{
    public class ReportingController : BaseController
    {
        // Create an instance of repository
        MilestoneManagementRepository repository = new MilestoneManagementRepository();

        #region Milestone reports
        [Authorize(Roles = "Administrator")]
        public ActionResult MilestoneReport()
        {
            MilestoneViewModel model = new MilestoneViewModel();
            
            // Setup model
            model.PageType = "Menu";
            SetupCommon(model, string.Empty, "Name");

            // Build params for report
            model.FromDate = DateTime.Now.AddMonths(-1);
            model.ToDate = DateTime.Now;
            model.PageSize = 10;
            model.PageIndex = 1;
            model.SortBy = "Username";

            repository.GetModel(model, 0, 0);

            return View(model);
        }

        [HttpPost]
        public ActionResult GetMilestone(int sourceId, int userId, bool checkRange, string range, string startDate, string endDate)
        {
            MilestoneViewModel model = new MilestoneViewModel();

            // Build params
            if (checkRange)
            {
                startDate = range.Split('|')[0];
                endDate = range.Split('|')[1];
            }

            string[] arrStart = startDate.Split('/');
            string[] arrEnd = endDate.Split('/');

            DateTime start = DateTime.Parse(string.Format("{0}-{1}-{2}", arrStart[2], arrStart[1], arrStart[0]));
            model.FromDate = LeadTools.StartOfDay(start);

            DateTime end = DateTime.Parse(string.Format("{0}-{1}-{2}", arrEnd[2], arrEnd[1], arrEnd[0]));
            model.ToDate = LeadTools.EndOfDay(end);

            // Setup model
            repository.GetModel(model, sourceId, userId);
            
            return Json(model.ReportList, JsonRequestBehavior.AllowGet);
        }
        #endregion
        
    }
}
